package com.tadpole.music.presenter.me;

import android.widget.Toast;

import com.google.gson.JsonObject;
import com.tadpole.music.bean.me.MoNiErrorBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.MoNiListIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * 模拟训练统计
 * Created By 孙岩
 * on 2020/12/26
 */
public class MoNiListPresenter extends BasePresenter<MoNiListIView> {
    @SuppressWarnings("unchecked")
    public void getMoNiList(int user_id){
        if(isViewAttached()){
            String url = UrlConfig.getMoniList + user_id;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    List<MoNiErrorBean.DataBean> list = new ArrayList<>();
                                    JSONArray array = new JSONArray(obj.getString("data"));
                                    for(int i=0;i<array.length();i++){
                                        JSONObject JsonObject = new JSONObject(array.get(i).toString());

                                        List<MoNiErrorBean.DataBean.MonthClassStaticBean> monthClassList = new ArrayList<>();
                                        JSONArray monthClassArray = new JSONArray(JsonObject.getString("month_class_static"));
                                        for(int j=0;j<monthClassArray.length();j++){
                                            JSONObject mothClassObj = new JSONObject(monthClassArray.get(j).toString());
                                            MoNiErrorBean.DataBean.MonthClassStaticBean monthClassStaticBean = new MoNiErrorBean.DataBean.MonthClassStaticBean(
                                                    JsonParseUtil.getInt(mothClassObj,"class_id"),
                                                    JsonParseUtil.getStr(mothClassObj,"name"),
                                                    JsonParseUtil.getStr(mothClassObj,"rate"),
                                                    JsonParseUtil.getInt(mothClassObj,"err_no")
                                            );
                                            monthClassList.add(monthClassStaticBean);
                                        }


                                        JSONObject mothDataObj = new JSONObject(JsonObject.getString("month_data"));
                                        List<Integer> daysList = new ArrayList<>();
                                        if(mothDataObj.isNull("days")){
                                            daysList = null;
                                        }else{
                                            JSONArray daysArray = new JSONArray(mothDataObj.getString("days"));
                                            for(int a=0;a<daysArray.length();a++){
                                                daysList.add(daysArray.getInt(a));
                                            }
                                        }
                                        MoNiErrorBean.DataBean.MonthDataBean monthDataBean = new MoNiErrorBean.DataBean.MonthDataBean(
                                                JsonParseUtil.getStr(mothDataObj,"month"),
                                                daysList
                                        );

                                        JSONObject monthDataObj = new JSONObject(JsonObject.getString("month_data_static"));
                                        MoNiErrorBean.DataBean.MonthDataStaticBean monthDataStaticBean = new MoNiErrorBean.DataBean.MonthDataStaticBean(
                                                JsonParseUtil.getStr(monthDataObj,"rate"),
                                                JsonParseUtil.getInt(monthDataObj,"total_err_no"),
                                                JsonParseUtil.getInt(monthDataObj,"total_questions"),
                                                JsonParseUtil.getInt(monthDataObj,"total_time")
                                        );

                                        JSONObject totalObj = new JSONObject(JsonObject.getString("total"));
                                        MoNiErrorBean.DataBean.TotalBean totalBean = new MoNiErrorBean.DataBean.TotalBean(
                                                JsonParseUtil.getStr(totalObj,"rate"),
                                                JsonParseUtil.getInt(totalObj,"total_err_no"),
                                                JsonParseUtil.getInt(totalObj,"total_questions"),
                                                JsonParseUtil.getInt(totalObj,"total_time")
                                        );

                                        MoNiErrorBean.DataBean dataBean = new MoNiErrorBean.DataBean(
                                                monthDataBean,
                                                monthDataStaticBean,
                                                JsonParseUtil.getStr(JsonObject,"name"),
                                                JsonParseUtil.getInt(JsonObject,"rank_id"),
                                                totalBean,
                                                monthClassList
                                        );

                                        list.add(dataBean);
                                    }
                                    getView().showMoNiList(list);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
